package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: zhao
 * @Date: 2020/12/2 19:55
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;

    /**
     * 分页查询供应商
     *
     * @param page
     * @param rows
     * @return
     */
    @Override
    public List<Supplier> getSupplier(Integer page, Integer rows) {
        PageHelper.startPage(page, rows);
        List<Supplier> supplierList = supplierDao.getSupplier();
        return supplierList;
    }

    /**
     * 供应商添加或修改
     *
     * @param supplier
     * @return
     */
    @Override
    public ServiceVO addSupplier(Supplier supplier) {

        if (supplier.getSupplierId() == null) {
            //id为null，执行添加操作
            supplierDao.addSupplier(supplier);
        } else {
            //id不为null，执行修改操作
            supplierDao.updateSupplier(supplier);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }


    /**
     * 删除供应商（支持批量删除）
     *
     * @param ids
     * @return
     */
    @Override
    public ServiceVO deleteSupplier(String ids) {
        //Integer.valueOf(ids).intValue() 将string转换成int
        supplierDao.deleteSupplier(Integer.valueOf(ids).intValue());
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, null);
    }
}
